/**
 * Licensed under the Apache License Version 2.0.
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package net.interknit.knitter.devkit;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

/**
 *
 */
public class GeneratorTask extends Task
{
	public static final String TYPE_CMDLINE = "cmdline";
	public static final String TYPE_WAR = "war";
	public static final String TYPE_EAR = "ear";

	private String generateservicecode = "false";
	private String generateclientcode = "false";
	private String generateprojectlayout = "false";
	private String codegentype = codeGenType("");
	private String verbose = "false";
	private String outputlocation = outputLocation("");
	private String descriptorurl = "";

	private static String codeGenType(String input)
	{
		if(input != null && ! input.isEmpty())
		{
			if(input.equalsIgnoreCase(TYPE_WAR))
				return TYPE_WAR;
			else if(input.equalsIgnoreCase(TYPE_EAR))
				return TYPE_EAR;
		}

		return TYPE_CMDLINE;
	}

	private static String outputLocation(String input)
	{
		if(input != null && ! input.isEmpty())
		{
			return input;
		}
		else
		{
			return "./generated-code";
		}
	}

	/**
	 * @return the generateservicecode
	 */
	public String getGenerateservicecode()
	{
		return this.generateservicecode;
	}

	/**
	 * @param generateservicecode the generateservicecode to set
	 */
	public void setGenerateservicecode(String generateservicecode)
	{
		this.generateservicecode = generateservicecode;
	}

	/**
	 * @return the generateclientcode
	 */
	public String getGenerateclientcode()
	{
		return this.generateclientcode;
	}

	/**
	 * @param generateclientcode the generateclientcode to set
	 */
	public void setGenerateclientcode(String generateclientcode)
	{
		this.generateclientcode = generateclientcode;
	}

	/**
	 * @return the generateprojectlayout
	 */
	public String getGenerateprojectlayout()
	{
		return this.generateprojectlayout;
	}

	/**
	 * @param generateprojectlayout the generateprojectlayout to set
	 */
	public void setGenerateprojectlayout(String generateprojectlayout)
	{
		this.generateprojectlayout = generateprojectlayout;
	}

	/**
	 * @return the codegentype
	 */
	public String getCodegentype()
	{
		return codeGenType(this.codegentype);
	}

	/**
	 * @param codegentype the codegentype to set
	 */
	public void setCodegentype(String codegentype)
	{
		this.codegentype = codegentype;
	}

	/**
	 * @return the verbose
	 */
	public String getVerbose()
	{
		return this.verbose;
	}

	/**
	 * @param verbose the verbose to set
	 */
	public void setVerbose(String verbose)
	{
		this.verbose = verbose;
	}

	/**
	 * @return the outputlocation
	 */
	public String getOutputlocation()
	{
		return this.outputlocation;
	}

	/**
	 * @param outputlocation the outputlocation to set
	 */
	public void setOutputlocation(String outputlocation)
	{
		this.outputlocation = outputLocation(outputlocation);
	}

	/**
	 * @return the descriptorurl
	 */
	public String getDescriptorurl()
	{
		return this.descriptorurl;
	}

	/**
	 * @param descriptorurl the descriptorurl to set
	 */
	public void setDescriptorurl(String descriptorurl)
	{
		this.descriptorurl = descriptorurl;
	}

	@Override
	public void execute() throws BuildException
	{
		System.out.println(getClass().getName() + " attribute values: ");
		System.out.println("  " + "generateservicecode:   [" + generateservicecode + "]");
		System.out.println("  " + "generateclientcode:    [" + generateclientcode + "]");
		System.out.println("  " + "generateprojectlayout: [" + generateprojectlayout + "]");
		System.out.println("  " + "verbose:               [" + verbose + "]");
		System.out.println("  " + "outputlocation:        [" + outputlocation + "]");
		System.out.println("  " + "descriptorurl:         [" + descriptorurl + "]");
		System.out.println("  " + "codegentype:           [" + codegentype + "]");

		validate();
	}

	protected void validate() throws ValidationException
	{
		boolean valid = true;

		StringBuilder validationErrors = new StringBuilder("Validation errors:\n");
		if(! new Boolean(generateservicecode).booleanValue() && ! new Boolean(generateclientcode).booleanValue())
		{
			valid = false;
			validationErrors.append("- Exactly one of the generateservicecode (-s) or generateclientcode (-c) must be true" + "\n");
		}
		if(new Boolean(generateservicecode).booleanValue() && new Boolean(generateclientcode).booleanValue())
		{
			valid = false;
			validationErrors.append("- Exactly one of the generateservicecode (-s) or generateclientcode (-c) can be true" + "\n");
		}
		if(descriptorurl == null || descriptorurl.isEmpty())
		{
			valid = false;
			validationErrors.append("- descriptorurl (-u|-f) must be a non-null/non-empty value" + "\n");
		}

		if(! valid)
		{
			throw new ValidationException(validationErrors.toString());
		}
	}
}
